﻿using Microsoft.EntityFrameworkCore.ChangeTracking;
using SaiLing.Repository;
using SaiLing.SingleSignOn.Domain.Entities.System;
using SaiLing.SingleSignOn.Domain.Interfaces.System;
using System.Threading.Tasks;

namespace SaiLing.SingleSignOn.Repository.Providers.System
{
    public class AccountRepository : BaseRepository<Account>, IAccountRepository
    {
        public AccountRepository(IDatabaseFactory<ProjectDbContext> databaseFactory) : base(databaseFactory)
        {

        }
        public Task Edit(Account model)
        {
            return Task.Factory.StartNew(() =>
            {
                Account entity = base.GetByKey(model.Id).Result;
                if (entity == null)
                    throw new SaiLing.Exceptions.SaiLingException("未找到该条数据");
                EntityEntry<Account> entry = base.DataContext.Entry(entity);
                entity.Positions?.Clear();//导航属性必须先清空

                entry.Collection(a => a.Positions).CurrentValue = model.Positions;
                entry.Property(a => a.Email).CurrentValue = model.Email;
                entry.Property(a => a.IsApproved).CurrentValue = model.IsApproved;
                entry.Property(a => a.Name).CurrentValue = model.Name; ;
            });

        }
    }
}
